package jsyf.oa.web.contract;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jsyf.com.constant.SendConstant;
import jsyf.com.util.*;
import jsyf.oa.facade.contract.ReceivableFacade;
import jsyf.oa.web.sender.Sender;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@Api(value = "应收台账接口", description = "应收台账接口")
@SuppressWarnings("all")
@Controller
@RequestMapping("/receivable")
public class ReceivableController {
    @Resource
    private ReceivableFacade receivableFacade;

    @ApiOperation(
            value = "",
            notes = "应收详情页保存方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/receivableSave.json", method = RequestMethod.POST)
    public void receivableSave(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = receivableFacade.receivableSave(jsonData);
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond("保存成功"));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("保存失败"));
        }
    }
    @ApiOperation(
            value = "",
            notes = "还款录入页保存方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/saveAvailable.json", method = RequestMethod.POST)
    public void saveAvailable(HttpServletResponse response, @RequestBody String jsonData) {
        String result = receivableFacade.saveAvailable(jsonData);
        if ("".equals(result)) {
            result = BaseUtils.errorrRespond("请输入还款金额");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", result);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }
    @ApiOperation(
            value = "",
            notes = "应收详情页查询方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectContractById.json", method = RequestMethod.POST)
    public void selectContract(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.selectContractById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }
    @ApiOperation(
            value = "",
            notes = "应收消息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/receivableRemind.json", method = RequestMethod.POST)
    public void receivableRemind(HttpServletResponse response) {
        Boolean result = this.receivableFacade.receivableRemind();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond("true"));
    }
    @ApiOperation(
            value = "",
            notes = "应收消息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/receivableRemindYq.json", method = RequestMethod.POST)
    public void receivableRemindYq(HttpServletResponse response) {
        Boolean result = this.receivableFacade.receivableRemindYq();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond("true"));
    }
    @ApiOperation(
            value = "",
            notes = "还款页查询方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/getAcountRepaymentListById.json", method = RequestMethod.POST)
    public void getAcountRepaymentListById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.getAcountRepaymentListById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }

    @ApiOperation(
            value = "",
            notes = "查询附件",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/getUploadFileByContractId.json", method = RequestMethod.POST)
    public void getUploadFileByContractId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.getUploadFileByContractId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }
    @ApiOperation(
            value = "",
            notes = "保存附件",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/saveUploadFile.json", method = RequestMethod.POST)
    public void saveUploadFile(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.saveUploadFile(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }
    @ApiOperation(
            value = "",
            notes = "免息页查询方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/getInterestFreeListById.json", method = RequestMethod.POST)
    public void getInterestFreeListById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.getInterestFreeListById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }

    @ApiOperation(
            value = "",
            notes = "免息审核记录",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/getInterestFreeById.json", method = RequestMethod.POST)
    public void getInterestFreeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.getInterestFreeById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }
    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        boolean result = receivableFacade.applyAudit(jsonData);
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("审核失败"));
        }
    }
    @ApiOperation(
            value = "",
            notes = "免息保存方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/saveInterestFreeList.json", method = RequestMethod.POST)
    public void saveInterestFreeList(HttpServletResponse response, @RequestBody String jsonData) {
        String result = this.receivableFacade.saveInterestFreeList(jsonData);
        if ("".equals(result)) {
            result = BaseUtils.errorrRespond("");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", result);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }
    @ApiOperation(
            value = "",
            notes = "免息页审核保存方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/interestFreeApply.json", method = RequestMethod.POST)
    public void interestFreeApply(HttpServletResponse response, @RequestBody String jsonData) {
        String result = this.receivableFacade.interestFreeApply(jsonData);
        if ("".equals(result)) {
            result = BaseUtils.errorrRespond("");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", result);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @ApiOperation(
            value = "",
            notes = "还款页保存方法",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/saveAcountRepaymentListById.json", method = RequestMethod.POST)
    public void saveAcountRepaymentListById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.saveAcountRepaymentListById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }
    @ApiOperation(
            value = "",
            notes = "将快到期变成逾期",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/saveOverByRepayment.json", method = RequestMethod.POST)
    public void saveOverByRepayment(HttpServletResponse response) {
        JSONObject result = this.receivableFacade.saveOverByRepayment();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }

    @RequestMapping(value = "/selectContractAccountById.json", method = RequestMethod.POST)
    public void selectContractAccountById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.selectContractAccountById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }

    @RequestMapping(value = "/updateContractAccountMoney.json", method = RequestMethod.POST)
    public void updateContractAccountMoney(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.updateContractAccountMoney(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }
    @ApiOperation(
            value = "",
            notes = "应收台账列表",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectContractAccountList.json", method = RequestMethod.POST)
    public void selectContractAccountList(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.selectContractAccountList(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }
    @ApiOperation(
            value = "",
            notes = "应收台账列表",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectContractAccountListApp.json", method = RequestMethod.POST)
    public void selectContractAccountListApp(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.selectContractAccountListApp(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }
    /*
    导出应收台账
     */
    @RequestMapping(value = "/exportContractAccountList.json", method = RequestMethod.GET)
    public void exportContractAccountList(HttpServletResponse response, @RequestParam String checkedAccounts) throws Exception {
        //导出表名、列名
        String title = "应收台账";
        String[] rowsName = checkedAccounts.split(",");
        String[] cellName = checkedAccounts.split(",");
        for (int i = 0; i < rowsName.length; i++) {
            System.out.println(rowsName[i]);

            rowsName[i] = rowsName[i].substring(rowsName[i].lastIndexOf("-") + 1, rowsName[i].length());
        }
        System.out.println(rowsName);
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        List<JSONObject> jsonObjectList = receivableFacade.exportContractAccountList();
        for (int i = 0; i < jsonObjectList.size(); i++) {
            JSONObject jsonObject = jsonObjectList.get(i);
            objs = new Object[rowsName.length];

            if (StringUtils.isBlank(jsonObjectList.get(i).getString("customerCredit"))) {
                if (jsonObjectList.get(i).getString("haveOverdue").equals("1")) {
                    Integer overdueNo = Integer.parseInt(jsonObjectList.get(i).getString("haveOverdueNo"));
                    if (overdueNo <= 30) {
                        jsonObjectList.get(i).put("customerCredit", "B类：逾期1-30天");
                    } else if (31 < overdueNo && overdueNo <= 60) {
                        jsonObjectList.get(i).put("customerCredit", "B-A类：逾期31-60天");
                    } else {
                        jsonObjectList.get(i).put("customerCredit", "A类：逾期60天以上");
                    }
                } else {
                    jsonObjectList.get(i).put("customerCredit", "C类：无逾期");
                }
            }
            for (int j = 0; j < cellName.length; j++) {
                if (rowsName[j].equals("是否结清")) {
                    if (jsonObject.getString(cellName[j].substring(0, cellName[j].lastIndexOf("-"))).equals("1")) {
                        objs[j] = "是";
                    } else {
                        objs[j] = "否";
                    }
                } else {
                    objs[j] = jsonObject.getString(cellName[j].substring(0, cellName[j].lastIndexOf("-")));
                }
            }
            dataList.add(objs);
        }
        ExportExcel ex = new ExportExcel(title, rowsName, dataList, response);
        ex.export();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond("导出成功"));
    }

    @ApiOperation(
            value = "",
            notes = "短信发送",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/getRepaymentSender.json", method = RequestMethod.POST)
    public void getRepaymentSender(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = this.receivableFacade.getRepaymentSender(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    @ApiOperation(
            value = "",
            notes = "获取mainId进行判断",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/getRepaymentDate.json", method = RequestMethod.POST)
    public void getRepaymentDate(HttpServletResponse response) {
        List<JSONObject> result = this.receivableFacade.getRepaymentDate();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }
    @ApiOperation(
            value = "",
            notes = "判断快到期的客户",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/senderExpire.json", method = RequestMethod.POST)
    public void senderExpire(HttpServletResponse response) {
        Boolean flag = this.receivableFacade.senderExpire();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(String.valueOf(flag)));
    }


    @ApiOperation(
            value = "",
            notes = "获取mainId进行判断免息申请查看权限",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/interestLook.json", method = RequestMethod.POST)
    public void interestLook(HttpServletResponse response,@RequestBody String jsonData) {
        List<JSONObject> result = this.receivableFacade.interestLook(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }


    @ApiOperation(
            value = "",
            notes = "短信发送按钮",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/senderMessage.json", method = RequestMethod.POST)
    public void senderMessage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String mainId = jsonParams.getString("mainId");
        String comName = jsonParams.getString("comName");
        String customerTel = jsonParams.getString("customerTel");
        String content = jsonParams.getString("content");
        receivableFacade.senderMessage(mainId);
        send(comName, SendConstant.SENDER_PWD, customerTel, content);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond("发送成功"));
    }

    public void send(String comName, String comPwd, String phone, String content) {
        // 存放调用返回的结果,判断成功与否应该分析这个字符串
        String returnCode;
        // 这里修改成你自己的用户名和密码
        Sender sms = new Sender(comName, comPwd);
        // 这里修改成要发送的手机号码和发送内容s
        returnCode = sms.massSend(phone, content, null, null);
        String encode = null;
        try {
            encode = new String(returnCode.getBytes("GBK"), "GB2312");
            System.out.println(encode);
        } catch (Exception e) {
            System.out.println("异常");
        }
    }

}